// 键盘事件-移除节点
export default G6 => {
    G6.registerBehavior('delete-item', {
        getEvents() {
            return {
                'keydown': 'onKeydown'
            }
        },
        onKeydown(e) {
            const graph = this.graph;
            // 删除节点，将与改节点连接的后代节点也删除
            if (e.keyCode === 46) {
                // 获取所有选中节点元素
                const nodes = graph.findAllByState('node', 'nodeState:selected');
                if (nodes && nodes.length) {
                    const $node = nodes[0];
                    const nodeModel = $node.getModel();
                    graph.emit('before-node-removed', {
                        target: $node,
                        targetModel: nodeModel,
                        callback(confirm) {
                            if (confirm) {
                                graph.removeItem(nodeModel.id);
                                graph.emit('after-node-removed', $node);
                            }
                        }
                    })
                }
                // 删除选中的边
                const edges = graph.findAllByState('edge', 'edgeState:selected');
                if (edges && edges.length) {
                    const $edge = edges[0];
                    const edgeModel = $edge.getModel();
                    graph.emit('before-edge-removed', {
                        target: $edge,
                        targetModel: edgeModel,
                        callback(confirm) {
                            if (confirm) {
                                graph.removeItem(edgeModel.id);
                                graph.emit('after-edge-removed', $edge);
                            }
                        }
                    })
                }
            }
        }
    })
}